/*
 * Copyright 2007-2009 Perttu "celeron55" Ahola <celeron55@gmail.com>
 *
 * This file is part of jukumagic.
 *
 * jukumagic is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jukumagic is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with jukumagic.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "common.h"
#include "arc.h"

JArc::JArc():
m_x1(0),
m_y1(0),
m_x2(0),
m_y2(0),
m_xc(0),
m_yc(0)
{
}

JArc::JArc(JArc &arc)
{
	m_x1 = arc.m_x1;
	m_y1 = arc.m_y1;
	m_x2 = arc.m_x2;
	m_y2 = arc.m_y2;
	m_xc = arc.m_xc;
	m_yc = arc.m_yc;
}

JArc::~JArc()
{
}

JukuObject* JArc::MakeACopy()
{
	JArc *arc = new JArc(*this);
	return arc;
}

void JArc::WriteXML(TiXmlNode* root)
{
    TiXmlElement *element = new TiXmlElement("Arc");
    root->LinkEndChild(element);
    
	element->SetAttribute("x1", m_x1);
	element->SetAttribute("y1", m_y1);
	element->SetAttribute("x2", m_x2);
	element->SetAttribute("y2", m_y2);
	element->SetAttribute("xc", m_xc);
	element->SetAttribute("yc", m_yc);

	WriteBaseXML(element);
}

JukuObject* JArc::ReadFromXMLElement(TiXmlElement* element)
{
    JArc *arc = new JArc();

	element->Attribute("xc", &(arc->m_xc));
	element->Attribute("yc", &(arc->m_yc));
    int r;
    if(element->Attribute("r", &r)){
    	arc->m_x1 = arc->m_xc - r;
    	arc->m_x2 = arc->m_xc - r;
    	arc->m_y1 = arc->m_yc;
    	arc->m_y2 = arc->m_yc;
	}
	else{
		element->Attribute("x1", &(arc->m_x1));
		element->Attribute("y1", &(arc->m_y1));
		element->Attribute("x2", &(arc->m_x2));
		element->Attribute("y2", &(arc->m_y2));
	}

	arc->ReadBaseXML(element);

    return arc;
}


void JArc::Draw(wxDC &dc, wxWindow *window, wxPoint origin)
{
	dc.DrawArc(m_x1+origin.x, m_y1+origin.y, m_x2+origin.x, m_y2+origin.y,
			m_xc+origin.x, m_yc+origin.y);
}

BoardRect JArc::GetDrawArea()
{
	int r_max, t;
	r_max = sqrt(pow(m_x1 - m_xc, 2) + pow(m_y1 - m_yc, 2));
	t = sqrt(pow(m_x2 - m_xc, 2) + pow(m_y2 - m_yc, 2));
	if(t > r_max) r_max = t;
	return BoardRect(m_xc-r_max-ARC_MAX_LINE_WIDTH/2, m_yc-r_max-ARC_MAX_LINE_WIDTH/2,
			r_max*2+ARC_MAX_LINE_WIDTH, r_max*2+ARC_MAX_LINE_WIDTH);
}

void JArc::Move(int dx, int dy)
{
	m_x1 += dx;
	m_y1 += dy;
	m_x2 += dx;
	m_y2 += dy;
	m_xc += dx;
	m_yc += dy;
}

void JArc::Rotate(TurnDirection dir, BoardPoint origin)
{
	int ox1 = m_x1 - origin.m_x;
	int oy1 = m_y1 - origin.m_y;
	int ox2 = m_x2 - origin.m_x;
	int oy2 = m_y2 - origin.m_y;
	int oxc = m_xc - origin.m_x;
	int oyc = m_yc - origin.m_y;
	int nox1, noy1, nox2, noy2, noxc, noyc;
	if(dir==CCW){
		nox1 = oy1;
		nox2 = oy2;
		noxc = oyc;
		noy1 = -ox1;
		noy2 = -ox2;
		noyc = -oxc;
	}
	else{ //CW
		nox1 = -oy1;
		nox2 = -oy2;
		noxc = -oyc;
		noy1 = ox1;
		noy2 = ox2;
		noyc = oxc;
	}
	m_x1 = origin.m_x + nox1;
	m_y1 = origin.m_y + noy1;
	m_x2 = origin.m_x + nox2;
	m_y2 = origin.m_y + noy2;
	m_xc = origin.m_x + noxc;
	m_yc = origin.m_y + noyc;
}



